#include "global.h"

Global* Global::_instance = nullptr;

Global *Global::instance()
{
    if(_instance == nullptr)
    {
        _instance = new Global();
    }

    return _instance;
}

QString Global::paramTypeToString(ePBType type)
{
    return m_ParamTypeMap.value(type);
}

ePBType Global::stringToParamType(QString str)
{
    return m_ParamTypeMap.key(str);
}

QStringList Global::paramTypeStringList()
{
    return m_ParamTypeMap.values();
}

QString Global::accessToString(eParamOptionAccess access)
{
    return m_OptionAccessMap.value(access);
}

eParamOptionAccess Global::stringToAccess(QString str)
{
    return m_OptionAccessMap.key(str);
}

QStringList Global::accessStringList()
{
    return m_OptionAccessMap.values();
}

bool Global::loadConfig()
{
    QSettings settings(m_strConfigFileName,
                         QSettings::IniFormat);
    m_lastFileName = settings.value("lastFile", "").toString();


    return true;
}

bool Global::saveConfig()
{
    QSettings settings(m_strConfigFileName,
                         QSettings::IniFormat);
    settings.setValue("lastFile", m_lastFileName);


    return true;
}

Global::Global()
{
    m_ParamTypeMap[PBType_INT8] = "int8";
    m_ParamTypeMap[PBType_INT16] = "int16";
    m_ParamTypeMap[PBType_INT32] = "int32";
    m_ParamTypeMap[PBType_INT64] = "int64";
    m_ParamTypeMap[PBType_UINT8] = "uint8";
    m_ParamTypeMap[PBType_UINT16] = "uint16";
    m_ParamTypeMap[PBType_UINT32]= "uint32";
    m_ParamTypeMap[PBType_UINT64] = "uint64";
    m_ParamTypeMap[PBType_Float] = "float";
    m_ParamTypeMap[PBType_Double] = "double";
    m_ParamTypeMap[PBType_Date]= "date";
    m_ParamTypeMap[PBType_Time] = "time";
    m_ParamTypeMap[PBType_DateTime] = "datetime";

    m_OptionAccessMap[PO_WriteOnly]="WriteOnly";
    m_OptionAccessMap[PO_ReadOnly]="ReadOnly";
    m_OptionAccessMap[PO_ReadWrite]="ReadWrite";

    m_strConfigFileName = QApplication::applicationDirPath() + QDir::separator() + "config.ini";
}

QString Global::lastFileName() const
{
    return m_lastFileName;
}

void Global::setLastFileName(const QString &lastFileName)
{
    m_lastFileName = lastFileName;
}
